﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using Microsoft.Office.Interop.Visio;
using GeoVision.Addin.UI.HelpSupport;
using Microsoft.SqlServer.Types;
using System.Drawing;
using GeoVision.Addin.Helpers;
using GeoVision.Addin.Core;
using GeoVision.Addin.Core.ShapeCore;
using System.Windows.Controls;
using System.Threading;
using GeoVision.Addin.UI.Shared;
using GeoVision.Addin.UI.SQLSpatial;
using GeoVision.Addin.Core.Spatial;
using GeoVision.Addin.Core.DataManagement;
using System.IO;
using GeoVision.Addin.Core.Server;
using GeoVision.Addin.UI.Settings;


namespace GeoVision.Addin
{
    public partial class MainRibbon
    {
        GeoVision.Addin.UI.SQLSpatial.SQLForm sqlForm;
        SettingsForm settings;
        SQLSpatialVisioGenerator ssvGenerator;


        public static SHPFileVisioGenerator shpGenerator;

        private void MainRibbon_Load(object sender, RibbonUIEventArgs e)
        {
            
        }

        private Microsoft.Office.Interop.Visio.Page ActivePage
        {
            get
            {
                EnsureVisioHasDocument();
                return Globals.ThisAddIn.Application.ActivePage;
            }
        }

        private void EnsureVisioHasDocument()
        {
            if (Globals.ThisAddIn.Application.ActiveDocument == null)
                Globals.ThisAddIn.Application.Documents.Add("");
        }

        private void btnGeoFromSQL_Click(object sender, RibbonControlEventArgs e)
        {
            sqlForm = new UI.SQLSpatial.SQLForm();
            if (sqlForm.ShowDialog() == DialogResult.Cancel)
                return;
            string connStr = sqlForm.ConnectionString;
            string tableName = sqlForm.TableName;
            double reduce = sqlForm.Reduce;
            string geoFieldName = sqlForm.GeoFieldName;
            BindShapeForm bindForm = new BindShapeForm();
            VisShapeCreationInfo creationInfo = new VisShapeCreationInfo();
            creationInfo.Schema = sqlForm.Schema;
            creationInfo = bindForm.Execute(creationInfo);
            if (creationInfo != null)
            {
                ssvGenerator = new SQLSpatialVisioGenerator(ActivePage, connStr, tableName, geoFieldName, reduce, creationInfo);
                ssvGenerator.Draw();
            };
        }

        private void ctlAbout_Click(object sender, RibbonControlEventArgs e)
        {
            new FmAbout().ShowDialog();
        }
        private void ctlInsertGeoShapeFile_Click(object sender, RibbonControlEventArgs e)
        {
            ofd.Filter = "Shapefiles (*.shp)|*.shp";
            ofd.Title = "Open Shapefile for Import";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                BindShapeForm bindForm = new BindShapeForm();
                VisShapeCreationInfo creationInfo = new VisShapeCreationInfo();
                creationInfo = bindForm.Execute(creationInfo);
                if (creationInfo != null)
                {
                    shpGenerator = new SHPFileVisioGenerator(this.ActivePage, ofd.FileName, creationInfo);
                    shpGenerator.Draw();
                }
            }
        }

        private void btnSettingsRibbon_Click(object sender, RibbonControlEventArgs e)
        {
            settings = new SettingsForm();
            settings.Show();
        }

        private void ctlGeoFromBing_Click(object sender, RibbonControlEventArgs e)
        {
            TemporaryWebServer server = new TemporaryWebServer();
            server.Start();

            string location = string.Format("http://localhost:{0}/download.apsx?type=html", server.Port);
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = location;
            proc.Start();

        }

        private void button2_Click(object sender, RibbonControlEventArgs e)
        {
            //Todo:
        }
    }
}
